using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Odbc;
using System.Data.Common;
using System.Globalization;

public partial class _Default : System.Web.UI.Page 
{
    //TODO: add "Seclect your lang", universal strings included in all resources

    private Database db;
    /// <summary>
    /// Shows a form for generating resources or an XML spreadsheet
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        db = new Database();
        using(Database.Command cmd = db.CreateCommand()) {
            cmd.CommandText = "SELECT id, displayname FROM applications ORDER BY displayname ASC";
            Database.DataAdapter adapter = new Database.DataAdapter(cmd);
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            radApplication.DataSource = ds;
            radApplication.DataValueField = "id";
            radApplication.DataTextField = "displayname";
            if(!Page.IsPostBack) {
                radApplication.DataBind();
                if(radApplication.Items.Count > 0) {
                    radApplication.SelectedIndex = 0;
                    ShowLanguages(null, EventArgs.Empty);
                    mltNoApplications.ActiveViewIndex = 1;
                }
            }
        }
        txtThreshold.Text = Constants.DefaultThreshold.ToString();
        db.CloseConnection();
    }

    /// <summary>
    /// Shows the languages and bundles for the application selected.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void ShowLanguages(object sender, EventArgs e) {
        if(radApplication.SelectedValue == null) {
            return;
        }
        db = new Database();
        db.OpenConnection();
        using(Database.Command cmd = db.CreateCommand()) {
            cmd.CommandText = "SELECT languages, type FROM applications WHERE id = @p";
            cmd.AddParameterWithValue("@p", radApplication.SelectedValue);
            DbDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);
            string languages = string.Empty;
            while(reader.Read()) {
                languages = reader[0].ToString();
                switch(reader[1].ToString()) {
                    case "dotnet":
                    case "dotnetcf":
                        lblBundleName.Text = "Form name";
                        break;
                    case "java":
                    default:
                        lblBundleName.Text = "Bundle Name";
                        break;
                }
            }
            reader.Close();
            lstLanguages.Items.Clear();
            string[] langs = languages.Trim().Split(',');
            Array.Sort(langs);
            foreach(string abbrev in langs) {
                ListItem itm;
                try {
                    itm = new ListItem(CultureInfo.GetCultureInfo(abbrev).EnglishName, abbrev);
                } catch {
                    itm = new ListItem(abbrev);
                }
                itm.Selected = true;
                lstLanguages.Items.Add(itm);
            }
            if(lstLanguages.Items.Count == 0) {
                ListItem itm = new ListItem("No languages found", "");
                itm.Selected = true;
                lstLanguages.Items.Add(itm);
                lstLanguages.Enabled = false;
            } else {
                lstLanguages.Enabled = true;
            }
            cmd.CommandText = "SELECT DISTINCT bundlename FROM elements WHERE applicationid = @p ORDER BY bundlename";
            reader = cmd.ExecuteReader();
            lstBundles.Items.Clear();
            string bundleName;
            while(reader.Read()) {
                bundleName = reader[0].ToString();
                if(bundleName != null && bundleName.Length > 0) {
                    ListItem itm = new ListItem(bundleName);
                    itm.Selected = true;
                    lstBundles.Items.Add(itm);
                }
            }
            if(lstBundles.Items.Count == 0) {
                ListItem itm = new ListItem("All", "");
                itm.Selected = true;
                lstBundles.Items.Add(itm);
                lstBundles.Enabled = false;
            } else {
                lstBundles.Enabled = true;
            }
            reader.Close();
        }
        db.CloseConnection();
    }
}
